package com.htdata.iiot.api.tsdb;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.htdata.iiot.api.tsdb.pojo.DeviceNumber;
import com.htdata.iiot.api.tsdb.utils.QueryTSDB;
import com.htiiot.common.config.ConfigFactory;
import com.htiiot.common.db.DbConnectionPool;
import com.htiiot.common.db.JDBCUtils;

public class Main2 {
	private static Logger logger = LogManager.getLogger(Main2.class);
	private static DbConnectionPool pool;
	private static String tenantIds;
	private static int times=60;

	public static void main(String[] args) throws IOException {
		QueryTSDB tsdbq = new QueryTSDB();
		ConfigFactory.getConfigMgr();
		
		long startTime = 0;
		long endTime = 0;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date datestart = sdf.parse("2017-04-26 00:40:01");
			Date dateend = sdf.parse("2018-05-15 23:40:02");
			startTime = datestart.getTime();
			endTime = dateend.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		pool  = DbConnectionPool.getPool();
		
		String sql1 = " select * from (SELECT  T2.* FROM" + "  (SELECT" + "    T.*," + "    CASE" + "      WHEN @mid = struct_id"
				+ "      THEN @row := @row + 1" + "      ELSE @row := 1" + "    END rownum,"
				+ "    @mid := struct_id MID" + "  FROM" + "    (SELECT" + "      struct_id," + "      id,tenant_id,"
				+ "      SUM(1)" + "    FROM" + "      thing_device" + "      WHERE"
				+ "        deleted=0 AND TYPE=0" + "    GROUP BY struct_id,id,tenant_id"
				+ "    ORDER BY struct_id,id," + "      SUM(1) DESC) AS T) AS T2 ) t " ;
			sql1+=" where id= 91507";
		

		logger.info(sql1);
		Connection conn = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet executeQuery = null;
		PreparedStatement prepareStatement = null;
		int i=0;
		try {
			ps = conn.prepareStatement(sql1);
			rs = ps.executeQuery();
			while (rs.next()) {
				int deviceId = rs.getInt("id");
				int tenant_id = rs.getInt("tenant_id");

				String sql2 = "SELECT" + "  t1.id AS id," + "  t2.`name` AS name," + "  t3.`name` AS componentname,"
						+ "  t4.`name` AS devicename" + " FROM" + "  `thing_device_component_rel` t1,"
						+ "  `thing_data_stream` t2," + "  thing_component t3," + "  `thing_device` t4"
						+ " WHERE t1.`component_id` = t3.`id`" + "  AND t3.deleted = 0"
						+ "  AND t2.`component_id` = t3.`id`" + "  AND t1.deleted = 0" + "  AND t1.device_id = ?"
						+ "  AND t2.deleted = 0" + "  AND t2.tenant_id = ?" + "  AND t4.id = t1.`device_id`";
			//	logger.info(sql2);
				prepareStatement = conn.prepareStatement(sql2);
				prepareStatement.setInt(1, deviceId);
				prepareStatement.setInt(2, tenant_id);
				executeQuery = prepareStatement.executeQuery();
				while (executeQuery.next()) {
					String name = executeQuery.getString("name");
					long mId = executeQuery.getLong("id");
					String hexString = new DeviceNumber(tenant_id, deviceId, mId).toHexString();
					
					 tsdbq.deleteTsdb(hexString+"_"+name, startTime, endTime);
					 System.out.println("mId:"+mId+",name:"+name+":"+(++i));
				}
				
				System.out.println("--over");
				
			}
			System.in.read();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(ps, rs, conn);
			JDBCUtils.close(prepareStatement, executeQuery, null);
		}

		
	}


}